package com.heima.skydemo.config;

import com.aliyun.oss.OSSClient;
import com.heima.skydemo.InterceptorRegistry.JwtInterceptorRegistry;
import com.heima.skydemo.domin.common.AliOssProperties;
import com.heima.skydemo.utils.AliOssUtil;
import com.heima.skydemo.utils.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@Slf4j
public class MyMvcConfig implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptorRegistry JwtInterceptorRegistry;
    @Autowired
    private AliOssProperties aliOssProperties;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //配置 knife4j 的静态资源请求映射地址
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(JwtInterceptorRegistry).addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        log.info("扩展消息转换器....");
        //创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //需要为消息转化器设置一个对象转换器，对象转换器可以将java对象序列化json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        //将自己的消息转换器添加到容器中
        converters.add(0,converter);
    }
    @Bean
   public AliOssUtil aliOssUtil(){
        return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());
    }
}
